﻿package morn.core.components
{
    import flash.display.*;
    import flash.events.*;
    import morn.core.handlers.*;

    public class FrameClip extends Component implements IClip
    {
        protected var _autoStopAtRemoved:Boolean = true;
        protected var _mc:MovieClip;
        protected var _skin:String;
        protected var _frame:int;
        protected var _autoPlay:Boolean;
        protected var _interval:int;
        protected var _to:Object;
        protected var _complete:Handler;
        protected var _isPlaying:Boolean;

        public function FrameClip(param1:String = null)
        {
            this._interval = MornConfig.MOVIE_INTERVAL;
            this.skin = param1;
            return;
        }// end function

        override protected function initialize() : void
        {
            addEventListener(Event.ADDED_TO_STAGE, this.onAddedToStage);
            addEventListener(Event.REMOVED_FROM_STAGE, this.onRemovedFromStage);
            return;
        }// end function

        protected function onAddedToStage(event:Event) : void
        {
            if (this._autoPlay)
            {
                this.play();
            }
            return;
        }// end function

        protected function onRemovedFromStage(event:Event) : void
        {
            if (this._autoStopAtRemoved)
            {
                this.stop();
            }
            return;
        }// end function

        public function get skin() : String
        {
            return this._skin;
        }// end function

        public function set skin(param1:String) : void
        {
            if (this._skin != param1)
            {
                this._skin = param1;
                this.mc = App.asset.getAsset(param1);
            }
            return;
        }// end function

        public function get mc() : MovieClip
        {
            return this._mc;
        }// end function

        public function set mc(param1:MovieClip) : void
        {
            if (this._mc != param1)
            {
                if (this._mc && this._mc.parent)
                {
                    this._mc.stop();
                    removeChild(this._mc);
                }
                this._mc = param1;
                if (this._mc)
                {
                    this._mc.stop();
                    addChild(this._mc);
                    _contentWidth = this.mc.width;
                    _contentHeight = this.mc.height;
                    this.mc.width = width;
                    this.mc.height = height;
                }
            }
            return;
        }// end function

        override public function set width(param1:Number) : void
        {
            super.width = param1;
            if (this._mc)
            {
                this._mc.width = _width;
            }
            return;
        }// end function

        override public function set height(param1:Number) : void
        {
            super.height = param1;
            if (this._mc)
            {
                this._mc.height = _height;
            }
            return;
        }// end function

        public function get frame() : int
        {
            return this._frame;
        }// end function

        public function set frame(param1:int) : void
        {
            var _loc_2:Handler = null;
            this._frame = param1;
            if (this._mc)
            {
                this._frame = this._frame < this._mc.totalFrames && this._frame > -1 ? (this._frame) : (0);
                this._mc.gotoAndStop((this._frame + 1));
                if (this._to && ((this._mc.currentFrame - 1) == this._to || this._mc.currentLabel == this._to))
                {
                    this.stop();
                    this._to = null;
                    if (this._complete != null)
                    {
                        _loc_2 = this._complete;
                        this._complete = null;
                        _loc_2.execute();
                    }
                }
            }
            return;
        }// end function

        public function get totalFrame() : int
        {
            return this._mc ? (this._mc.totalFrames) : (0);
        }// end function

        public function get autoStopAtRemoved() : Boolean
        {
            return this._autoStopAtRemoved;
        }// end function

        public function set autoStopAtRemoved(param1:Boolean) : void
        {
            this._autoStopAtRemoved = param1;
            return;
        }// end function

        public function get autoPlay() : Boolean
        {
            return this._autoPlay;
        }// end function

        public function set autoPlay(param1:Boolean) : void
        {
            if (this._autoPlay != param1)
            {
                this._autoPlay = param1;
                if (this._autoPlay)
                {
                    this.play();
                }
                else
                {
                    this.stop();
                }
            }
            return;
        }// end function

        public function get interval() : int
        {
            return this._interval;
        }// end function

        public function set interval(param1:int) : void
        {
            if (this._interval != param1)
            {
                this._interval = param1;
                if (this._isPlaying)
                {
                    this.play();
                }
            }
            return;
        }// end function

        public function get isPlaying() : Boolean
        {
            return this._isPlaying;
        }// end function

        public function set isPlaying(param1:Boolean) : void
        {
            this._isPlaying = param1;
            return;
        }// end function

        public function play() : void
        {
            this._isPlaying = true;
            this.frame = this._frame;
            App.timer.doLoop(this._interval, this.loop);
            return;
        }// end function

        protected function loop() : void
        {
            var _loc_1:String = this;
            var _loc_2:* = this.frame + 1;
            _loc_1.frame = _loc_2;
            return;
        }// end function

        public function stop() : void
        {
            App.timer.clearTimer(this.loop);
            this._isPlaying = false;
            return;
        }// end function

        public function gotoAndPlay(param1:int) : void
        {
            this.frame = param1;
            this.play();
            return;
        }// end function

        public function gotoAndStop(param1:int) : void
        {
            this.stop();
            this.frame = param1;
            return;
        }// end function

        public function playFromTo(param1:Object = null, param2:Object = null, param3:Handler = null) : void
        {
            param1 = param1 || 0;
            this._to = param2 == null ? ((this._mc.totalFrames - 1)) : (param2);
            this._complete = param3;
            if (param1 is int)
            {
                this.gotoAndPlay(param1 as int);
            }
            else
            {
                this._mc.gotoAndStop(param1);
                this.gotoAndPlay((this._mc.currentFrame - 1));
            }
            return;
        }// end function

        override public function set dataSource(param1:Object) : void
        {
            _dataSource = param1;
            if (param1 is int || param1 is String)
            {
                this.frame = int(param1);
            }
            else
            {
                super.dataSource = param1;
            }
            return;
        }// end function

    }
}
